﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class Course
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class CourseList : List<Course>
    {
        public MyError Error { get; set; }
        public CourseList()
        {
        }
    }

    public class CourseDB
    {
        MyDatabase _db = new MyDatabase();

        public Course Save(Course Course)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", Course.Id);
                _param.Add("@CompanyId", Course.CompanyId);

                if (Course.Description != string.Empty)
                {
                    _param.Add("@Description", Course.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", Course.Name);


                _param.Add("@IsActive", Course.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_CourseInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    Course.Error.ErrorNo = 0;
                }
                else
                {
                    Course.Error.ErrorNo = 1;
                    Course.Error.ErrorMessage = "No row affect when saving school's course details.";
                }
            }
            catch (Exception Ex)
            {
                Course.Error.ErrorNo = 10002;
                Course.Error.ErrorLocation = "Course";
                Course.Error.ErrorMessage = Ex.Message;
                Course.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return Course;
        }

        public CourseList GetCourseList(int CompanyId)
        {
            CourseList retValue = new CourseList();

            try
            {
                DataTable _dt = new DataTable("tblCourse");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_CourseSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        Course _course = new Course();
                        FillData(_dr, _course);
                        retValue.Add(_course);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Course";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return retValue;
        }

        public Course GetCourse(int Id, int CompanyId)
        {
            Course retValue = new Course();

            try
            {

                DataTable _dt = new DataTable("tblCourse");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_CourseSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Course";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, Course Course)
        {
            Course.Id = Convert.ToInt32(aRow["Id"].ToString());
            Course.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            
            Course.Error = new MyError();

            if (aRow["Description"] != DBNull.Value)
            {
                Course.Description = aRow["Description"].ToString();
            }
            else
            {
                Course.Description = string.Empty;
            }

            Course.Name = aRow["Name"].ToString();
            Course.CompanyName = aRow["CompanyName"].ToString();

            Course.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            Course.Error.ErrorNo = 0;
        }
    }
}
